//
//  LKOpenSubordinateRateSettingViewController.m
//  Mideleague_Agent
//
//  Created by Zhiwang on 2020/6/19.
//  Copyright © 2020 Zhiwang. All rights reserved.
//

#import "LKOpenSubordinateRateSettingViewController.h"
#import "LKMyShopOpenRateSettingCell.h"
#import "LKRateSegmentCell.h"
@interface LKOpenSubordinateRateSettingViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)NSArray *titlesArray;
@property(nonatomic,strong)NSArray *ratesArray;
@property(nonatomic,copy)NSString *bottomStr;

@end

@implementation LKOpenSubordinateRateSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.bottomStr = @"";
    self.navigationItem.title = @"费率设置";
    

    [self setNavgationRightBtn];
    self.titlesArray = @[@"微信",@"支付宝",@"一千元以上(借记)",@"一千元以上(贷记)",@"一千元以下"];
    if (self.inforModel.wx_rate) {
        self.ratesArray = @[self.inforModel.wx_rate,self.inforModel.ali_rate,self.inforModel.ysf2_rate,self.inforModel.ysf_rate,self.inforModel.ysf1_rate];
    }else{
          self.ratesArray = @[@"",@"",@"",@"",@""];
        self.inforModel.is_card_liushui = @"1";
        self.inforModel.is_liushui = @"1";
        [self getRateInformationDetail];
    }

    [self createTableview];
    // Do any additional setup after loading the view.
}

-(void)setNavgationRightBtn{
    WS(weakSelf)
    LKBaseButton *rightBtn = [LKBaseButton buttonWithFrame:CGRectMake(0, 0, 64, 24) type:UIButtonTypeCustom title:@"提交" titleColor:COLOR_HEX(0x333333) layercorne:13 font:[UIFont fontWithName:@"PingFangSC-Regular" size: 16] backgroudColor:[UIColor clearColor] AndActionBlock:^(UIButton * _Nonnull btn) {
        LKMyShopOpenRateSettingCell *wxcell = [weakSelf.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        weakSelf.inforModel.wx_rate = wxcell.textField.text;
        weakSelf.inforModel.bank_rate = wxcell.textField.text;
        LKMyShopOpenRateSettingCell *alicell = [weakSelf.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
        weakSelf.inforModel.ali_rate = alicell.textField.text;
        LKMyShopOpenRateSettingCell *ysf2cell = [weakSelf.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]];
        weakSelf.inforModel.ysf2_rate = ysf2cell.textField.text;
        LKMyShopOpenRateSettingCell *ysfcell = [weakSelf.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:3]];
        weakSelf.inforModel.ysf_rate = ysfcell.textField.text;
        LKMyShopOpenRateSettingCell *ysf1cell = [weakSelf.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:4]];
        weakSelf.inforModel.ysf1_rate = ysf1cell.textField.text;
        if ([NSString isBlankString:weakSelf.inforModel.wx_rate]) {
            [SVProgressHUD showInfoWithStatus:@"请输入微信费率"];
            return ;
        }
        if ([weakSelf.inforModel.wx_rate integerValue] < 0 || [weakSelf.inforModel.wx_rate integerValue] > 1) {
            [SVProgressHUD showInfoWithStatus:@"请输入正确的微信费率"];
            return ;
        }
        if ([NSString isBlankString:weakSelf.inforModel.ali_rate]) {
            [SVProgressHUD showInfoWithStatus:@"请输入支付宝费率"];
            return ;
        }
        if ([weakSelf.inforModel.ali_rate integerValue] < 0 || [weakSelf.inforModel.ali_rate integerValue] > 1) {
            [SVProgressHUD showInfoWithStatus:@"请输入正确的支付宝费率"];
            return ;
        }
        if ([NSString isBlankString:weakSelf.inforModel.ysf2_rate]) {
            [SVProgressHUD showInfoWithStatus:@"请输入一千元以上(借记)费率"];
            return ;
        }
        if ([weakSelf.inforModel.ysf2_rate integerValue] < 0 || [weakSelf.inforModel.ysf2_rate integerValue] > 1) {
            [SVProgressHUD showInfoWithStatus:@"请输入正确的一千元以上(借记)费率"];
            return ;
        }
        if ([NSString isBlankString:weakSelf.inforModel.ysf_rate]) {
            [SVProgressHUD showInfoWithStatus:@"请输入一千元以上(贷记)费率"];
            return ;
        }
        if ([weakSelf.inforModel.ysf_rate integerValue] < 0 || [weakSelf.inforModel.ysf_rate integerValue] > 1) {
            [SVProgressHUD showInfoWithStatus:@"请输入正确的一千元以上(贷记)费率"];
            return ;
        }
        if ([NSString isBlankString:weakSelf.inforModel.ysf1_rate]) {
            [SVProgressHUD showInfoWithStatus:@"请输入一千元以下费率"];
            return ;
        }
        if ([weakSelf.inforModel.ysf_rate integerValue] < 0 || [weakSelf.inforModel.ysf_rate integerValue] > 1) {
            [SVProgressHUD showInfoWithStatus:@"请输入正确的一千元以下费率"];
            return ;
        }
        if (weakSelf.modelBlock) {
            weakSelf.modelBlock(weakSelf.inforModel);
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
        
//        NSDictionary *param = [weakSelf.inforModel properties_aps];
//        [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MySuboradinateAddUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
//
//        } success:^(BOOL isSuccess, id  _Nullable responseObject) {
//            if ([responseObject[@"code"] isEqualToString:@"success"]) {
//                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
//            }else{
//                [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
//            }
//
//
//        } failure:^(NSString * _Nullable errorMessage) {
//
//        }];

    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    if (self.isCheckStatu) {
        rightBtn.hidden = YES;
    }else{
        rightBtn.hidden = NO;
    }
}
-(void)createTableview{
    self.tableView = [LKBaseTableview setTableviewWithFrame:CGRectZero style:UITableViewStyleGrouped registercellClasss:@[@"LKMyShopOpenRateSettingCell",@"LKRateSegmentCell"]];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _tableView.backgroundColor = viewDefualtColor;
    [self.view addSubview:_tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.view).offset(NAVIGATION_BAR_HEIGHT);
        make.bottom.equalTo(self.view).offset(0);
    }];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
    }
    self.tableView.scrollEnabled = NO;
    [self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titlesArray.count + 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 5) {
        LKRateSegmentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKRateSegmentCell"];
        cell.customSwitch.tag = 10000;
        cell.header = @"支付费率返佣";
        if ([self.inforModel.is_liushui integerValue]) {
            [cell.customSwitch setOn:YES];
        }else{
            [cell.customSwitch setOn:NO];
        }
        if (self.isCheckStatu) {
            cell.customSwitch.enabled = NO;
        }
        [cell.customSwitch addTarget:self action:@selector(switchTypeChange:) forControlEvents:UIControlEventValueChanged];
        return cell;
    }
    if (indexPath.section == 6) {
        LKRateSegmentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKRateSegmentCell"];
        cell.customSwitch.tag = 10001;
        cell.header = @"联名卡返佣";
        if ([self.inforModel.is_card_liushui integerValue]) {
            [cell.customSwitch setOn:YES];
        }else{
            [cell.customSwitch setOn:NO];
        }
        if (self.isCheckStatu) {
            cell.customSwitch.enabled = NO;
        }
        [cell.customSwitch addTarget:self action:@selector(switchTypeChange:) forControlEvents:UIControlEventValueChanged];
        return cell;
    }
    LKMyShopOpenRateSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKMyShopOpenRateSettingCell"];
    cell.headerLabel.text = self.titlesArray[indexPath.section];
    cell.values = self.ratesArray[indexPath.section];
    cell.textField.enabled = NO;
    if (self.isCheckStatu) {
        cell.textField.enabled = NO;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0f;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *header = [[UIView alloc] init];
    for (UIView *subViews in header.subviews) {
        [subViews removeFromSuperview];
    }
    if (section == 2) {
        LKBaseLabel *headerLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"云闪付费率(借记卡、贷记卡)" textColor:COLOR_HEX(0x666666) font:[UIFont fontWithName:@"PingFangSC-Regular" size:16] textAlignment:NSTextAlignmentLeft];
        [header addSubview:headerLabel];
        [headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(header);
            make.left.equalTo(header).offset(10);
        }];
    }
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

     if (section == 0 || section == 1) {
         return 10.0f;
     }
     if (section == 2) {
         return 60.0f;
     }
    return 0.00000000001f;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc] init];
    if (section == 6) {
//        LKBaseLabel *footerLabel = [LKBaseLabel labelWithFrame:CGRectZero text:@"- 提交方费率 -" textColor:COLOR_HEX(0x666666) font:[UIFont fontWithName:@"PingFangSC-Regular" size:14] textAlignment:NSTextAlignmentLeft];
//        [footerView addSubview:footerLabel];
//        [footerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.equalTo(footerView);
//            make.top.equalTo(footerView).offset(10);
//        }];
//        LKBaseLabel *footerIntroLabel = [LKBaseLabel labelWithFrame:CGRectZero text:self.bottomStr textColor:COLOR_HEX(0x666666) font:[UIFont fontWithName:@"PingFangSC-Regular" size:12] textAlignment:NSTextAlignmentCenter];
//        footerIntroLabel.numberOfLines = 0;
//        [footerView addSubview:footerIntroLabel];
//        [footerIntroLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.equalTo(footerView);
//            make.top.equalTo(footerLabel.mas_bottom).offset(10);
//            make.left.equalTo(footerView).offset(20);
//        }];
//        return footerView;
    }
    return footerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    if (section == 4) {
//        return 120;
//    }
    return 0.0000000001f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

/// 按钮更改显示
/// @param swit 按钮
-(void)switchTypeChange:(UISwitch *)swit{
    if (swit.tag == 10000) {
        if (swit.on) {
            self.inforModel.is_liushui = @"1";
        }else{
            self.inforModel.is_liushui = @"0";
        }
    }else{
        if (swit.on) {
            self.inforModel.is_card_liushui = @"1";
        }else{
            self.inforModel.is_card_liushui = @"2";
        }
    }
}
/// 获取底部费率显示
-(void)getRateInformationDetail{
    NSDictionary *param = @{};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:MySuboradinateAddRateUrl header:[LKUserInforTool account].token parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        if ([responseObject[@"code"] isEqualToString:@"success"]) {
            NSDictionary *alldict = responseObject[@"data"];
            NSDictionary *dict = alldict[@"agent"];
            self.inforModel.wx_rate = dict[@"wx_rate"];
            self.inforModel.ali_rate = dict[@"ali_rate"];
            self.inforModel.ysf2_rate = dict[@"ysf2_rate"];
            self.inforModel.ysf_rate = dict[@"ysf_rate"];
            self.inforModel.ysf1_rate = dict[@"ysf1_rate"];
        self.ratesArray = @[self.inforModel.wx_rate,self.inforModel.ali_rate,self.inforModel.ysf2_rate,self.inforModel.ysf_rate,self.inforModel.ysf1_rate];
            [self.tableView reloadData];
        }else{
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
        }

    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
